﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="StringUtility.cs" company="">
//   
// </copyright>
// <summary>
//   The string utility.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Libium.Utilities
{
    using Libium.Core;

    /// <summary>
    ///   The string utility.
    /// </summary>
    public static class StringUtility
    {
        #region Public Methods and Operators

        /// <summary>
        /// The extract file path.
        /// </summary>
        /// <param name="text">
        /// The text. 
        /// </param>
        /// <param name="action">
        /// The action. 
        /// </param>
        /// <returns>
        /// The <see cref="string"/> . 
        /// </returns>
        /// <exception cref="NotExistsException">
        /// </exception>
        public static string ExtractFilePath(string text, NotExistsAction action = NotExistsAction.ThrowException)
        {
            var match = RegularExpressions.FilePath.Match(text);

            if (match.Success)
            {
                return match.Value;
            }

            if (action == NotExistsAction.ThrowException)
            {
                throw new NotExistsException("File path doesn't exist in the text");
            }

            return null;
        }

        /// <summary>
        /// The extract url.
        /// </summary>
        /// <param name="text">
        /// The text. 
        /// </param>
        /// <param name="action">
        /// The action. 
        /// </param>
        /// <returns>
        /// The <see cref="string"/> . 
        /// </returns>
        /// <exception cref="NotExistsException">
        /// </exception>
        public static string ExtractUrl(string text, NotExistsAction action = NotExistsAction.ThrowException)
        {
            var match = RegularExpressions.Uri.Match(text);

            if (match.Success)
            {
                return match.Value;
            }

            if (action == NotExistsAction.ThrowException)
            {
                throw new NotExistsException("Url doesn't exist in the text");
            }

            return null;
        }

        /// <summary>
        /// The get word count.
        /// </summary>
        /// <param name="str">
        /// The str. 
        /// </param>
        /// <returns>
        /// The <see cref="int"/> . 
        /// </returns>
        public static int GetWordCount(string str)
        {
            var result = 0;
            var inWord = false;
            for (var index = 0; index < str.Length; index++)
            {
                if (char.IsWhiteSpace(str, index))
                {
                    inWord = false;
                }
                else
                {
                    if (!inWord)
                    {
                        inWord = true;
                        result++;
                    }
                }
            }

            return result;
        }

        #endregion
    }
}